package com.ltt.controller;

import com.alibaba.fastjson.JSON;
import com.ltt.annotation.NoAuth;
import com.ltt.common.ErrorMessage;
import com.ltt.common.UserThreadLocal;
import com.ltt.dao.pojo.Harvest;
import com.ltt.dao.pojo.Market;
import com.ltt.dao.pojo.Order;
import com.ltt.dao.pojo.User;
import com.ltt.dao.vo.OrderVo;
import com.ltt.dao.vo.Result;
import com.ltt.dao.vo.params.OrderParams;
import com.ltt.service.HarvestService;
import com.ltt.service.MarketService;
import com.ltt.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/5 15:38
 */
@CrossOrigin(origins = "*")
@Api(tags = "商品模块")
@RestController
@RequestMapping("/market")
public class MarketController {
    @Autowired
    private MarketService marketService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private HarvestService harvestService;

    @ApiOperation("获取所有商品")
    @GetMapping("/findAll")
    @NoAuth
    public Result getAllMarket() {
        List<Market> allMarket = marketService.getAllMarket();
        return Result.successWithData(allMarket);
    }

    /**
     * @param orderParams
     * @return com.ltt.dao.vo.Result
     * @description: 用户传递商品id和地址id
     * 通过商品id找到商品，通过openid找到用户信息
     * 比较积分看是否足够
     * 足够了就进行积分的减且进行订单的生成
     * 不够就返回积分不够
     */
    @ApiOperation("购买商品")
    @PostMapping("/buy")
    public Result buyMarket(@RequestBody OrderParams orderParams) {
        Market market = marketService.getMarket(orderParams.getMarketId());

        String openid = UserThreadLocal.get().getOpenid();
        User user = userInfoService.findUserById(openid);

        Harvest harvest = harvestService.findHarvestById(openid, orderParams.getHarvestId());
        if (harvest == null) {
            return Result.failed(ErrorMessage.DATA_IS_WRONG.message());
        }

        String address = harvest.toJson();

        if (user.getIntegral() >= market.getIntegral()) {
            user.setIntegral(user.getIntegral() - market.getIntegral());

            Order order = new Order();
            order.setOpenid(openid);
            order.setMarketId(orderParams.getMarketId());
            order.setAddress(address);

            userInfoService.changeJiFen(user);
            marketService.buyMarket(order);

            return Result.successWithoutData();
        } else {
            return Result.failed(ErrorMessage.INTEGRAL_LACK.message());
        }
    }

    @ApiOperation("查询用户的所有订单")
    @PostMapping("/findOrderById")
    public Result findOrderById() {
        List<OrderVo> allOrder = marketService.findAllOrderById(UserThreadLocal.get().getOpenid());

        for (OrderVo orderVo : allOrder) {
            orderVo.setHarvest(JSON.parseObject(orderVo.getAddress(), Harvest.class));
        }

        return Result.successWithData(allOrder);
    }

    public static void main(String[] args) {
        String str = "{master:'李天泰', phone:145641, region:'士大夫沙发上的吧', detailed:'dasfsdsdfgggg'}";
        Harvest harvest = JSON.parseObject(str, Harvest.class);
        System.out.println(harvest.toString());
    }
}
